package com.baigao.springboot.controller;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
 






import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
@Controller
public class Upload2Controller {
    @Value("${file.path}")
    private String filePath;
 
    @ResponseBody
    @RequestMapping(value = "/apk_upload" ,method = RequestMethod.POST)
    public Map<String, Object> uploadApkFile(HttpServletRequest request,HttpServletResponse response)
            throws Exception {
        request.setCharacterEncoding("UTF-8");

        Map<String, Object> json = new HashMap<String, Object>();
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        
        /** 页面控件的文件流* */
        MultipartFile multipartFile = null;
        Map map =multipartRequest.getFileMap();
         for (Iterator i = map.keySet().iterator(); i.hasNext();) {
                Object obj = i.next();
                multipartFile=(MultipartFile) map.get(obj);

               }
        /** 获取文件的后缀* */
        String filename = multipartFile.getOriginalFilename();

        //a.jpg
        String extName = multipartFile.getOriginalFilename().substring(filename.lastIndexOf("."));
 
        String fileName = UUID.randomUUID().toString() + extName;
        
        File file2 = new File(filePath + fileName);
        if(!file2.exists()){
            //先得到文件的上级目录，并创建上级目录，在创建文件
            file2.getParentFile().mkdir();
            
            // 或  file2.mkdir();
        }
        FileCopyUtils.copy(multipartFile.getInputStream(), new FileOutputStream(new File(filePath + fileName)));
//        return fileName;
        json.put("newVersionName", fileName);
        json.put("fileMd5", "0e9045435");
        json.put("message", "应用上传成功");
        json.put("status", true);
        json.put("filePath", filePath);
        return json;
    }
    
    
    @ResponseBody
    @RequestMapping("/filesUpload")  
    public Map<String, Object> filesUpload(@RequestParam("files") MultipartFile[] files) { 
    	Map<String, Object> json = new HashMap<String, Object>();
    	
    	List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
        String path = filePath;
        //判断file数组不能为空并且长度大于0  
        if(files!=null&&files.length>0){  
            //循环获取file数组中得文件  
            for(int i = 0;i<files.length;i++){  
            	Map<String, Object> json2 = new HashMap<String, Object>();
                MultipartFile file = files[i]; 
             // 文件保存路径  
                String savePath = path + file.getOriginalFilename();
                //保存文件  
                saveFile(file, path); 
                
                json2.put("name", file.getOriginalFilename());
                json2.put("savePath", savePath);
                list.add(json2);
            }  
        }  
        // 重定向  
        json.put("newVersionName", list.toString());
        json.put("message", "应用上传成功");
        json.put("status", true);
        System.out.println("============="+json.toString());
        return json;  
    }
    
    /*** 
     * 保存文件 
     * @param file 
     * @return 
     */  
    private boolean saveFile(MultipartFile file, String path) {  
        // 判断文件是否为空  
        if (!file.isEmpty()) {  
            try {  
                File filepath = new File(path);
                if (!filepath.exists()) 
                    filepath.mkdirs();
                // 文件保存路径  
                String savePath = path + file.getOriginalFilename();  
                // 转存文件  
                file.transferTo(new File(savePath));  
                return true;  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
        return false;  
    }  

}
